package beanAction;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import modele.beanEntity.Recipe;
import modele.mapper.RecipeMapper;
@ManagedBean(name="recipemanagerbean")
@ViewScoped
public class RecipeManagerBean implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4851207864419301508L;
	private List<Recipe> recipeList;
	private Recipe currentRecipe;
	private UIComponent uiComponent;
	private List<String> cookTypes;
	
	@ManagedProperty(value="#{searchRecipe}")
	private SearchRecipeBean searchRecipeBean;
	
	
	
	public RecipeManagerBean(){
		RecipeMapper rm;
		try {
			rm = new RecipeMapper();
			cookTypes = Recipe.getTypes();
			recipeList = rm.getRecipeList();
			currentRecipe = getFirstRecipe();
			
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	public void attrListener(ActionEvent e){
		uiComponent= e.getComponent();
	}
	
	private Recipe getFirstRecipe(){
		if(!recipeList.isEmpty()){
			return recipeList.get(0);
		}
		return new Recipe();
	}
	
	public void saveRecipe(){
		if(currentRecipe != null){
			RecipeMapper rm;
			try {
				rm = new RecipeMapper();
				if(rm.recipeExists(currentRecipe.getId())){
					if(rm.updateRecipe(currentRecipe)){
						searchRecipeBean.refreshCookTypes();
						recipeList = rm.getRecipeList();
						FacesContext.getCurrentInstance().addMessage(uiComponent.getClientId(), new FacesMessage(FacesMessage.SEVERITY_INFO,"Update success !",null));
					}else{
						FacesContext.getCurrentInstance().addMessage(uiComponent.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR,"Update failure !",null));
					}
				}else{
					if(rm.createRecipe(currentRecipe)){
						searchRecipeBean.refreshCookTypes();
						recipeList = rm.getRecipeList();
						currentRecipe = getFirstRecipe();
						FacesContext.getCurrentInstance().addMessage(uiComponent.getClientId(), new FacesMessage(FacesMessage.SEVERITY_INFO,"Recipe added successfully !",null));
					}else{
						FacesContext.getCurrentInstance().addMessage(uiComponent.getClientId(), new FacesMessage(FacesMessage.SEVERITY_INFO,"Adding failed !",null));
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				FacesContext.getCurrentInstance().addMessage(uiComponent.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR,"Update failure !",null));
				e.printStackTrace();
			}
			
		}
	}
	
	public void delete(){
		
		RecipeMapper rm;
		try {
			rm = new RecipeMapper();
			if(rm.deleteRecipe(currentRecipe)){
				recipeList = rm.getRecipeList();
				currentRecipe = getFirstRecipe();
				FacesContext.getCurrentInstance().addMessage(uiComponent.getClientId(), new FacesMessage(FacesMessage.SEVERITY_INFO,"Recipe deleted successfully!",null));
			}else{
				FacesContext.getCurrentInstance().addMessage(uiComponent.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR,"Deleting failed !",null));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			FacesContext.getCurrentInstance().addMessage(uiComponent.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR,"Deleting failed !",null));
			e.printStackTrace();
		}
		
	}



	public List<Recipe> getRecipeList() {
		return recipeList;
	}



	public void setRecipeList(List<Recipe> recipeList) {
		this.recipeList = recipeList;
	}



	public Recipe getCurrentRecipe() {
		return currentRecipe;
	}



	public void setCurrentRecipe(Recipe currentRecipe) {
		if(currentRecipe == null){
			this.currentRecipe= new Recipe();
		}else{
			this.currentRecipe = currentRecipe;
		}
	}



	public List<String> getCookTypes() {
		return cookTypes;
	}



	public void setCookTypes(List<String> cookTypes) {
		this.cookTypes = cookTypes;
	}



	public SearchRecipeBean getSearchRecipeBean() {
		return searchRecipeBean;
	}



	public void setSearchRecipeBean(SearchRecipeBean searchRecipeBean) {
		this.searchRecipeBean = searchRecipeBean;
	}
}
